<?php

namespace App\HttpController\XAppSystem\Test\Web;


use App\Common\OA\OA;
use App\Common\Sys;
use App\Common\WX\Api\OfficialAccount;
use App\HttpController\ControllerBase;
use App\Model\t_s_accsuper_config;
use App\Model\t_wx_user;
use App\Redis\SubscribeEvent;
use App\X\EHelper;
use EasySwoole\Http\Message\UploadFile;
use EasySwoole\HttpClient\HttpClient;
use Swoole\Coroutine\Http\Client;
use XWX\Common\H;
use XWX\Common\WebClient\WebClient;
use XWX\Common\WebClient\WebClientCo;

/**
 * http://php.x.siyoumi.com/test/web_0/test
 *
 */
class Test extends ControllerBase
{
    public function index()
    {
        $this->redis6380()->setEx('tttt', 5, time());

//        $this->funcSetX('1');
//        $app = OfficialAccount::funcIns($this->funcGetX__config(), true);
//        $wx_from = 'o4uoFt3KIw1mzb3Sa49KBQ303ttY';
//        $r = $app->funcSendMsg48($wx_from, '测试：' . time());
//        var_dump($r);
//
//        $url = 'https://www.baidu.com';
//        $url = 'http://192.168.0.88/';
//        $url = 'https://www.runoob.com/docker/docker-install-nginx.html';

//        $client = new Client('www.baidu.com', 443, true);
//        $client->setMethod('GET');
//        $client->execute('/');
//
//        var_dump($client);


//        $test = new HttpClient();
//        $test = new WebClientCo();
//        $test->setConnectTimeout(1);
//        $test->setUrl($url);
////        $test->setConnectTimeout()
//        $x = $test->get();
//        var_dump($x);

//        $this->funcSetX('1');
//        $entity_config = t_s_accsuper_config::funcLoad($this->db(), '1');
//        $entity_config->aconfig_partnerId_sub = '123';
//
//        $this->commonR()->setData('x', $entity_config);
//        $x = $entity_config->access_token();
////        $r = OfficialAccount::funcIns($entity_config)->funcRefreshAccessToken();
//
////        $this->redis6380()->setEx('xxx', 20, '123');
//        $e = t_wx_user::funcGetWxUser('o4uoFt3KIw1mzb3Sa49KBQ303ttY');
//
//        $this->commonR()->setData('r', $x);
//        $this->commonR()->setData('e', $e);

//        $url = 'https://api.weixin.qq.com/cgi-bin/token?appid=wx7805055f85dd3d56&grant_type=client_credential&secret=b6bdbedbb93aaf17d6f2c3b23949e752';
////        $url = 'http://192.168.2.88/';
//        $client = new WebClient($url);
//        $client->setConnectTimeout(20);
//        $res = $client->get();
//
//        $this->commonR()->setData('res', $res->getBody());

        $this->funcWriteCommonR();
    }


    public function test_msg48()
    {
        $this->funcSetX('1');

        $app = OfficialAccount::funcIns($this->funcGetX__config());

        $wx_from = 'o4uoFt3KIw1mzb3Sa49KBQ303ttY';
        $r = $app->msg48()->funcSendText($wx_from, time());

        $this->funcWriteErrR($r);
        $this->funcWriteCommonR();
    }

    public function test_openssl_decrypt()
    {
        $appid = 'wx4f4bc4dec97d474b';
        $sessionKey = 'tiihtNczf5v6AKRyjwEUhQ==';
        $encryptedData = "CiyLU1Aw2KjvrjMdj8YKliAjtP4gsMZM
                QmRzooG2xrDcvSnxIMXFufNstNGTyaGS
                9uT5geRa0W4oTOb1WT7fJlAC+oNPdbB+
                3hVbJSRgv+4lGOETKUQz6OYStslQ142d
                NCuabNPGBzlooOmB231qMM85d2/fV6Ch
                evvXvQP8Hkue1poOFtnEtpyxVLW1zAo6
                /1Xx1COxFvrc2d7UL/lmHInNlxuacJXw
                u0fjpXfz/YqYzBIBzD6WUfTIF9GRHpOn
                /Hz7saL8xz+W//FRAUid1OksQaQx4CMs
                8LOddcQhULW4ucetDf96JcR3g0gfRK4P
                C7E/r7Z6xNrXd2UIeorGj5Ef7b1pJAYB
                6Y5anaHqZ9J6nKEBvB4DnNLIVWSgARns
                /8wR2SiRS7MNACwTyrGvt9ts8p12PKFd
                lqYTopNHR1Vf7XjfhQlVsAJdNiKdYmYV
                oKlaRv85IfVunYzO0IKXsyl7JCUjCpoG
                20f0a04COwfneQAGGwd5oa+T8yO5hzuy
                Db/XcxxmK01EpqOyuxINew==";
        $iv = 'r7BXXKkLb8qrSNn05n0qiA==';

        $aesKey = base64_decode($sessionKey);
        $aesIV = base64_decode($iv);
        $aesCipher = base64_decode($encryptedData);

        $result = openssl_decrypt($aesCipher, "AES-128-CBC", $aesKey, 1, $aesIV);

        $this->commonR()->setData('res', $result);
    }


    public function redis_close()
    {
        $app = new SubscribeEvent();
        $app->funcEventExpiredClose();
    }


    public function redis_psubscribe()
    {
        go(function ()
        {
            $app = new SubscribeEvent();
            $app->funcEventExpired();
        });


        $this->commonR()->errmsg = '开启成功';
        $this->funcWriteCommonR();
    }


    public function upload_img()
    {
        /** @var UploadFile $file */
        $file = $this->request()->getUploadedFile('file0');
        if ($file == null)
        {
            return $this->funcWriteErr(-8100, '文件不存在');
        }


        $file_type = $file->getClientMediaType();

        $allowed_file_extensions = ["png", "jpg", "gif", "jpeg"];
        $file_extensions = explode('/', $file_type)[1];
        if (!in_array($file_extensions, $allowed_file_extensions))
        {
            return $this->funcWriteErr(-8200, '只能上传指定格式的文件');
        }


        //文件目录
        $ym = H::now()->format('Ym');
        $dir_path = EHelper::funcGetConf('SYS.upload');
        $dir_path = "{$dir_path}/{$ym}/";
        if (!is_dir($dir_path))
        {
            mkdir($dir_path);
        }

        $file_name = H::funcGetOrderID() . '.' . $file_extensions;
        $file_val = "{$ym}/{$file_name}";

        $full_path = $dir_path . $file_name;
        if (!$file->moveTo($full_path))
        {
            return $this->funcWriteErr(-8500, '文件上传失败');
        }


        //http://img.x.siyoumi.com/201910/201910282221245db6f9648a98411176.jpeg
        //201910/201910282221245db6f9648a98411176.jpeg
        $img = [
            'id' => $file_val,
            'url' => OA::funcGetFileUrl($file_val),
        ];
        $this->commonR()->setData('img', $img);


        $this->funcWriteCommonR();
    }
}